package android.dj.lbscamera.activities;

import android.app.TabActivity;
import android.content.Intent;
import android.dj.lbscamera.R;
import android.dj.lbscamera.utils.Constants;
import android.dj.lbscamera.utils.UseUtils;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;

public class LBSTabHost extends TabActivity implements OnTabChangeListener {

	private TabHost mTabHost;
	private TabWidget mTabWidget;
	private int mCurrentDisplayTabId = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.tab_host_view);
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabWidget = (TabWidget) findViewById(android.R.id.tabs);
		mTabHost.setOnTabChangedListener(this);
		init();
	}

	private final int TAB_RES_ID[][] = {
			{ R.drawable.bg_near_postcard_selected,
					R.drawable.bg_near_postcard, R.drawable.bg_near_postcard },
			{ R.drawable.bg_main_postcard,
					R.drawable.bg_main_postcard_selected,
					R.drawable.bg_main_postcard },
			{ R.drawable.bg_my_postcard, R.drawable.bg_my_postcard,
					R.drawable.bg_my_postcard_selected }, };
	private static final int TAB_CHILD_ID = R.id.main_activity_tab_image;

	public void onTabChanged(String tabId) {
		int tabID = Integer.valueOf(tabId);

		for (int i = 0; i < 3; i++) {
			View v = mTabWidget.getChildAt(i);
			if (v != null) {
				v.findViewById(TAB_CHILD_ID).setBackgroundResource(
						TAB_RES_ID[i][tabID]);
			}
		}
	}

	private void init() {
		int i = 0;

		setIndicator(R.drawable.bg_near_postcard, "", i++, new Intent(this,
				PageNear.class));

		setIndicator(R.drawable.bg_main_postcard_selected, "", i++, new Intent(
				this, PageMain.class));

		setIndicator(R.drawable.bg_my_postcard, "", i++, new Intent(this,
				PageMyPostcard.class));

		mTabHost.setCurrentTab(1);

	}

	private void setIndicator(int bgNearPostcard, String string, int i,
			Intent intent) {

		View localView = LayoutInflater.from(this.mTabHost.getContext())
				.inflate(R.layout.tab_widget_view, null);
		if (i == 1) {
			localView.setLayoutParams(new LayoutParams(LBSTabHost.this
					.getWindow().getWindowManager().getDefaultDisplay()
					.getWidth() * 200 / 480, LayoutParams.WRAP_CONTENT));
		} else {
			localView.setLayoutParams(new LayoutParams(LBSTabHost.this
					.getWindow().getWindowManager().getDefaultDisplay()
					.getWidth() * 140 / 480, LayoutParams.WRAP_CONTENT));
		}
		((RelativeLayout) localView.findViewById(R.id.main_activity_tab_image))
				.setBackgroundResource(bgNearPostcard);
		String str = String.valueOf(i);

		TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(str)
				.setIndicator(localView).setContent(intent);
		mTabHost.addTab(localTabSpec);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		int tabId = UseUtils.getPreferenceData(LBSTabHost.this, Constants.NAME,
				Constants.KEY, 1);
		mTabHost.setCurrentTab(tabId);
	}

}
